package com.edu.framework.websocket.handler;

import cn.hutool.core.util.StrUtil;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.highlights.HighlightsRequest;
import com.edu.framework.websocket.message.highlights.HighlightsVO;
import com.edu.framework.websocket.message.tclass.ClassRequest;
import com.edu.framework.websocket.service.HighlighsService;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class HighlightsRequestHandler implements IHandler<HighlightsRequest> {

    @Resource
    private HighlighsService wsHighlighsService;

    @Override
    public void execute(WebSocketSession session, HighlightsRequest message) {
        if (message == null) {
            return;
        }
        Map<String, String> data = message.getData();
        // 存储的是roomId
        String classId = data.get(ClassRequest.CLASS_ID_KEY);
        Response response = null;

        if (StrUtil.isBlank(classId)){
            response = Response.fail(HighlightsRequest.TYPE, "班级id不能为空");
        } else {
            response = wsHighlighsService.getHighlightsVOS(Long.parseLong(classId));
        }

        try {
            WebSocketHolder.send(session,response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public String getType() {
        return HighlightsRequest.TYPE;
    }




}
